/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.maxato.xmlservices;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * http://www.maxithlon.com/maxi-xml/world.php
 * @author Fam Gloudemans
 */
public class GetXMLData {

    public GetXMLData() {
    }

    @SuppressWarnings("resource")
	void getDataAthletes(String cookie) {
        try {
            URL anotherUrl = new URL("http://www.maxithlon.com/maxi-xml/athletes.php");
            URLConnection connection = anotherUrl.openConnection();
            connection.setRequestProperty("Cookie", cookie);
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String lineRead = "";
            // Read all available lines of data from the URL and print them to screen.
            String alltekst = "";
            while ((lineRead = reader.readLine()) != null) {
                alltekst = alltekst + lineRead;
            }
            DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
            java.util.Date date = new java.util.Date();
            FileOutputStream fos = new FileOutputStream("xml/athletes/" + dateFormat.format(date) + ".xml");
            new PrintStream(fos, true, "UTF-8").println(alltekst);
            reader.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Get the XML data from the URL. 
     * typeData is the data you want, for example if you fill in World you get the World data.
     * This is the list with values we use:
     * -World
     * -Team
     * -Calendar
     * @param cookie
     * @param typeData 
     */
    @SuppressWarnings("resource")
	void getData(String cookie, String typeData) {
        try {
            URL anotherUrl = new URL("http://www.maxithlon.com/maxi-xml/" + typeData + ".php");
            URLConnection connection = anotherUrl.openConnection();
            connection.setRequestProperty("Cookie", cookie);
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String lineRead = "";
            // Read all available lines of data from the URL and print them to screen.
            String alltekst = "";
            while ((lineRead = reader.readLine()) != null) {
                alltekst = alltekst + lineRead;
            }
            FileOutputStream fos = new FileOutputStream("xml/" + typeData + ".xml");
            new PrintStream(fos, true, "UTF-8").println(alltekst);
            reader.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    String getCompetitionData(String cookie, String typeData) {
        try {
            URL anotherUrl = new URL("http://www.maxithlon.com/maxi-xml/competition.php?");
            URLConnection connection = anotherUrl.openConnection();
            connection.setRequestProperty("Cookie", cookie);
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String lineRead = "";
            // Read all available lines of data from the URL and print them to screen.
            String alltekst = "";
            while ((lineRead = reader.readLine()) != null) {
                alltekst = alltekst + lineRead;
            }
            return alltekst;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
            return null;
    }

}
